package com.example.thread.d9CompletableFuture2.d2RunAsync;
import com.example.thread.d9CompletableFuture2.CommonUtils;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class SupplyAsyncDemo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CommonUtils.printThreadLog("main start");

        CompletableFuture<String> newsFuture = CompletableFuture.supplyAsync(() -> {
            CommonUtils.printThreadLog("异步任务开始");
            return CommonUtils.readFile("hello world");
        });

        CommonUtils.printThreadLog("here not blocked main continue");
        String news = newsFuture.get(); // 方法执行到这里，会阻塞，等待多线程返回的结果
        System.out.println(news);
        CommonUtils.printThreadLog("main end");
    }
}
